package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RmUserDept;
import com.ruoyi.system.service.IRmUserDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户和部门管理关联Controller
 * 
 * @author ruoyi
 * @date 2020-02-02
 */
@Controller
@RequestMapping("/system/depts")
public class RmUserDeptController extends BaseController
{
    private String prefix = "system/dept";

    @Autowired
    private IRmUserDeptService rmUserDeptService;

    @RequiresPermissions("system:dept:view")
    @GetMapping()
    public String dept()
    {
        return prefix + "/dept";
    }

    /**
     * 查询用户和部门管理关联列表
     */
    @RequiresPermissions("system:dept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RmUserDept rmUserDept)
    {
        startPage();
        List<RmUserDept> list = rmUserDeptService.selectRmUserDeptList(rmUserDept);
        return getDataTable(list);
    }

    /**
     * 导出用户和部门管理关联列表
     */
    @RequiresPermissions("system:dept:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RmUserDept rmUserDept)
    {
        List<RmUserDept> list = rmUserDeptService.selectRmUserDeptList(rmUserDept);
        ExcelUtil<RmUserDept> util = new ExcelUtil<RmUserDept>(RmUserDept.class);
        return util.exportExcel(list, "dept");
    }

    /**
     * 新增用户和部门管理关联
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户和部门管理关联
     */
    @RequiresPermissions("system:dept:add")
    @Log(title = "用户和部门管理关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RmUserDept rmUserDept)
    {
        return toAjax(rmUserDeptService.insertRmUserDept(rmUserDept));
    }

    /**
     * 修改用户和部门管理关联
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        RmUserDept rmUserDept = rmUserDeptService.selectRmUserDeptById(userId);
        mmap.put("rmUserDept", rmUserDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户和部门管理关联
     */
    @RequiresPermissions("system:dept:edit")
    @Log(title = "用户和部门管理关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RmUserDept rmUserDept)
    {
        return toAjax(rmUserDeptService.updateRmUserDept(rmUserDept));
    }

    /**
     * 删除用户和部门管理关联
     */
    @RequiresPermissions("system:dept:remove")
    @Log(title = "用户和部门管理关联", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rmUserDeptService.deleteRmUserDeptByIds(ids));
    }
}
